package org.example.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mysql.jdbc.StringUtils;
import org.example.entity.Provider;
import org.example.excel.ProviderExcel;
import org.example.excel.ProviderExcelReadListener;
import org.example.exception.ErrorEnum;
import org.example.exception.ServiceException;
import org.example.mapper.ProviderMapper;
import org.example.param.ProviderParam;
import org.example.service.ProviderService;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

@Service
public class ProviderServicelmpl implements ProviderService{
    @Resource
    ProviderMapper providerMapper;

    @Override
    public Map<String, Object> save(Provider provider) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("name", provider.getName());
        if (providerMapper.exists(queryWrapper)) {
            throw new ServiceException(ErrorEnum.MATERIAL_EXISTS);
        }
        providerMapper.insert(provider);
        return success();
    }

    @Override
    public Map<String, Object> page(ProviderParam param) {
        IPage<Provider> page = new Page<>(param.getCurrent(), param.getSize());
        QueryWrapper queryWrapper = new QueryWrapper();
        if (!StringUtils.isNullOrEmpty(param.getProviderName())) {
            queryWrapper.like("name", param.getProviderName());
        }
        if (!StringUtils.isNullOrEmpty(param.getLicenseNumber())) {
            queryWrapper.like("license_number", param.getLicenseNumber());
        }
        if (!StringUtils.isNullOrEmpty(param.getContactPerson())) {
            queryWrapper.like("contact_person", param.getContactPerson());
        }
        page =  providerMapper.selectPage(page, queryWrapper);
        return success(page);
    }

    @Override
    public Map<String, Object> disable(Provider provider) {
        providerMapper.updateById(provider);
        return success();
    }

    @Resource
    JdbcTemplate jdbcTemplate;

    @Override
    public Map<String, Object> importExcel(MultipartFile file) {
        ProviderExcelReadListener listener = new ProviderExcelReadListener();
        try {
            EasyExcel.read(file.getInputStream(), ProviderExcel.class, listener).sheet().doRead();
            List<ProviderExcel> list = listener.getList();
            String sql = "insert into provider (name, license_number,contact_person,contact_phone,address) values (?,?,?,?,?)";
            jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
                @Override
                public void setValues(PreparedStatement ps, int i) throws SQLException {
                    ProviderExcel provider = list.get(i);
                    ps.setObject(1, provider.getName());
                    ps.setObject(2, provider.getLicenseNumber());
                    ps.setObject(3, provider.getContactPerson());
                    ps.setObject(4, provider.getContactPhone());
                    ps.setObject(5, provider.getAddress());
                }
                @Override
                public int getBatchSize() {
                    return list.size();
                }
            });
            return success();
        } catch (IOException e) {
            throw new ServiceException(ErrorEnum.EXCEL_IMPORT_ERROR);
        }
    }

    @Override
    public Map<String, Object> delete(Provider provider){
        if (provider == null || provider.getId() == null) {
            throw new ServiceException(ErrorEnum.ACCOUNT_NOT_EXISTS);
        }
        int rowsAffected = providerMapper.deleteById(provider.getId());
        if (rowsAffected == 0) {
            throw new ServiceException(ErrorEnum.DELETE_ERROR);
        }

        return success();
    }
}
